package com.toutiao

import java.io.File
import com.typesafe.config._

import com.toutiao.issue.EventStore

import org.apache.log4j.Logger
import org.apache.log4j.PropertyConfigurator

import com.toutiao.issue.IssueProcess
import com.toutiao.nssue.NssueProcess
import com.toutiao.vssue.VssueProcess

class AppProp(
    val mem:Map[String,String]){
  
  def apply(key:String) = mem(key)
  
}

object ReesApp extends App {

  val cc = Array(
    "ES_NAME",
    "ES_URI",
    "ES_PORT",
    "ES_INDEX",
    "ES_TYPE",

    "ES_CONTEXT_NAME",
    "ES_CONTEXT_URI",
    "ES_CONTEXT_PORT",
    "ES_CONTEXT_INDEX",
    "ES_CONTEXT_TYPE",

    "ES_MAP_NAME",
    "ES_MAP_URI",
    "ES_MAP_PORT",
    "ES_MAP_INDEX",
    "ES_MAP_TYPE",

    "TOP_S",
    "MIN_COUNT",
    
    "LOCATION_DATE",
    "TIME_SIZE",
    "TIME_STEP",

    "ES_DOC_NAME",
    "ES_DOC_URI",
    "ES_DOC_PORT",
    "ES_DOC_INDEX",
    "ES_DOC_TYPE",

    "ES_ISSUE_NAME",
    "ES_ISSUE_URI",
    "ES_ISSUE_PORT",
    "ES_ISSUE_INDEX",
    "ES_ISSUE_TYPE",

    "DB_ROOT",
    "DB_CACHE",
    
    "LOCATION_VU",
    "LOCATION_DOC_DATA",

    "MIN_V",
    "MIN_S",
    "MODE",
    
    "LOCATION_MEASURE",
    "DOC_NUM",
    
    "LOCATION_TITLE_MEASURE",
    "LOCATION_W_MEASURE",
    
    "SERVER_PORT",
    
    "CLOSE_DATE",
    
    "ZOOKEEPER",
    "CRAWL_TOPIC1",
    "CRAWL_TOPIC2",
    "CRAWL_TOPIC1_ACK",
    "CRAWL_TOPIC2_ACK",
    "GROUP_ID",
    
    "SIM_TOPIC1",
    "SIM_TOPIC2",
    "BROKERS",
    
    "DOCID_NAME",
    "SIMID_NAME",
    
    "ID_URI"    
    )
      
  var location_log4j = "log4j.properties"
  var location_sim = "SimIssue.properties"
  
  if (args.size == 2) {
    location_log4j = args(0);
    location_sim = args(1);
  }
  
  PropertyConfigurator.configure(location_log4j)
  val _log = Logger.getLogger(this.getClass)
  
  println("Hello Rees")
  
  val c = ConfigFactory.parseFile(new File(location_sim))
  def initAppProp = {
    val m = cc.map(v => {
      (v,c.getString(v))
    }).toMap
    new AppProp(m)
  }
  
  val prop = initAppProp
  
  val m = prop("MODE").toInt

  // 第一代, 内存, ES输入输出
  if (m == 1) {
    IssueProcess.run(prop) 
  }
  
  // 第二代, LevelDB, ES输入输出
  if (m == 2){
    NssueProcess.run(prop)
  }
  
  // 第三代, LevelDB, Kafka输入输出
  if (m == 3) {
    VssueProcess.run(prop)
  }
  
  // 调试用Server
  if (m == 4){
    val s = new ScorerServer(prop)
    s.run
  }
  
}

